<template>
    <div>
      <el-form :model="ruleForm" :rules="rules" class="login-container" label-position="left" label-width="0px" ref="loginForm">
        <!--<h3 class="login_title">login</h3>-->
        <div class="m-logo">
          <img src="https://vuejs.org/images/logo.png">
        </div>
        <el-form-item prop="username">
          <el-input type="text" v-model="ruleForm.username" auto-complete="off" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="ruleForm.password" auto-complete="off" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item style="width: 100%">
          <el-button type="primary"  @click="submitForm" style="width: 100%">登录</el-button>
        </el-form-item>
        <el-form-item style="width: 100%">
          <el-button type="primary"  @click="$router.push('/register')" style="width: 100%">去创建账号</el-button>
        </el-form-item>
      </el-form>
    </div>
</template>

<script>
import store from '../store'

export default {
  name: 'Login',
  data () {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // console.log('success submit!')
          this.$axios.post('/login', this.ruleForm).then(response => {
            // console.log('headers:' + response.headers)
            const jwt = response.headers.authorization // 获取token
            const userInfo = response.data.data // 获取用户信息
            // 提交保存
            this.$store.commit('SET_JWT', jwt)
            this.$store.commit('SET_USERINFO', userInfo)

            // console.log('jwt:' + this.$store.getters.getJWT)
            // console.log('userInfo:' + this.$store.getters.getUserInfo)
            this.$router.push('/home', () => { this.countUnreadNotice() })
          })
        } else { // 前端验证失败
          // console.log('前端验证失败')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    toResetPassword () {
      this.$router.push('/password_reset')
    },
    countUnreadNotice () {
      this.$axios.get('/notification/count_unread', {
        params: { userId: this.$store.state.userInfo.id },
        headers: { Authorization: this.$store.state.jwt }
      }).then(response => {
        const unreadNum = response.data.data
        if (unreadNum > 0) {
          // console.log(unreadNum)
          store.commit('set_new_message', 'yes')
          this.$notify.info({
            title: '通知',
            message: '你有' + unreadNum + '条未读消息'
          })
        }
      })
    }
  },
  mounted () {
    if (this.$store.state.loggedIn) {
      this.$router.push('/home')
    }
  }
}
</script>

<style scoped>
  /*logo图片样式*/
  .m-logo img{
    width: 68px;
    padding-left: 133px;
    padding-bottom: 20px;
  }
  /*登录表单容器样式*/
  .login-container {
    border-radius: 15px;
    background-clip: padding-box;
    margin: 120px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }
</style>
